package hust.rpc.serialize.json;

import hust.rpc.serialize.Serializer;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * Json序列化器
 */
@Slf4j
public class JsonSerializer implements Serializer {

    @Override
    public byte[] serialize(Object obj) {
        String jsonStr = JsonUtils.toJson(obj);
        return jsonStr.getBytes(StandardCharsets.UTF_8);
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> clazz) {
        // log.info("bytes -> {}", bytes);
        return JsonUtils.fromJson(new String(bytes, StandardCharsets.UTF_8), clazz);
    }
}
